package a66weding.com.jiehuntong.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.xutils.x;

import java.util.HashMap;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.base.MyBaseFragmentActivity;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MarriageProposalBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.MessageEvent;
import a66weding.com.jiehuntong.common.api.Url;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.FileUtil;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.common.utils.stubar.StatusBarUtil;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.pager.IContant;
import a66weding.com.jiehuntong.widgets.CircularImage;

/**
 * Created by admin on 2016/7/8.
 */
public class JobDetail extends MyBaseFragmentActivity implements IContant,View.OnClickListener,OnNetResultListener{
    public static String TASK_FLAG = "task_flag";
    private int job_Title = -1;
    //设置标题;
    private String title ;
    private TextView job_detail_photoname;
    //编辑
    //接受对象
    private MarriageProposalBean.DataBean dataBean;
    //任务地址
    private TextView bookPlace ;
    //备注内容
    private TextView bookNotice;
    //预订单
    private TextView bugMoney;
    //剩余天数
    private TextView days;
    private TextView tian;
    //是否完成任务
    private TextView isDone;
    //任务指派员
    private TextView assiment_member;
    //编辑
    private TextView task_editor_tv;
    //
    private TextView remark_content_tv;
    //头像
    private CircularImage circularImage;
    //头像的名字
    private String HEADName;
    private Bitmap  headBitmap;
    //是从哪个界面过来的
    private int flag;
    //图片地址
    private String picture;
    //下一个next
    private ImageView job_detail_nextto;
    //备注
    private TextView notice;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initView() {
        create(R.layout.activity_job_details);
        StatusBarUtil.setColor(this,getResources().getColor(R.color.job_detail_bg_color),1);
        job_detail_photoname = (TextView) findViewById(R.id.job_detail_photoname);
        task_editor_tv = (TextView) findViewById(R.id.task_editor_tv);
        task_editor_tv.setOnClickListener(this);
        bookPlace = (TextView) findViewById(R.id.job_detail_book_place);
        bookNotice = (TextView) findViewById(R.id.job_detail_remark_content_tv);
        bugMoney = (TextView) findViewById(R.id.job_detail_Consumption_expected_money_tv);
        days = (TextView) findViewById(R.id.tian_shu_tv);
        isDone = (TextView) findViewById(R.id.job_detail_isdone);
        assiment_member = (TextView) findViewById(R.id.job_detail_missionPersonnel_name_tv);
        remark_content_tv = (TextView) findViewById(R.id.job_detail_remark_content_tv);
        circularImage = (CircularImage) findViewById(R.id.job_detail_photo);
        job_detail_nextto = (ImageView) findViewById(R.id.job_detail_nextto);
        notice = (TextView) findViewById(R.id.job_detail_remark_content_tv);
        tian = (TextView) findViewById(R.id.tian);
    }
    @Override
    public void initData() {
        Intent intent = getIntent();
        //接受上面传递过来的对象
        dataBean = (MarriageProposalBean.DataBean) intent.getSerializableExtra(TASK_FLAG);
        flag = intent.getIntExtra("flag",0);
        picture = intent.getStringExtra("headpictrue");
        changContent(dataBean);
        HEADName = dataBean.getWeddingBookID()+".jpg";
        headBitmap =   FileUtil.getBitmapFromDisk(HEADName,this);
        if(headBitmap!=null){
            circularImage.setImageBitmap(headBitmap);
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //点击编辑;
            case R.id.task_editor_tv:
              Intent intent = new Intent(JobDetail.this,JobDetail_Edit_Activity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(TASK_FLAG,dataBean);
                intent.putExtras(bundle);
                startActivityForResult(intent,1);
                break;

        }
    }

    private void changContent(final MarriageProposalBean.DataBean dataBean ){
        job_detail_photoname.setText(dataBean.getBookTitle());
        if(dataBean.getBookPlace().length()!=0){
            bookPlace.setText(dataBean.getBookPlace());
        }
        bookNotice.setText(dataBean.getNotice());
        bugMoney.setText("¥" + dataBean.getBudgetMoney() + "");
        days.setText(dataBean.getDays());
        assiment_member.setText(dataBean.getNvc_Name() != null ? dataBean.getNvc_Name() : " ");
        remark_content_tv.setText(dataBean.getRemarks() != null ? dataBean.getRemarks() : " ");
        if(picture!=null){
            x.image().bind(circularImage,picture, ImageOptionsUtil.getOptions92_92());
        }
        String day = dataBean.getDays();
        if (!TextUtils.isEmpty(day)){
            if (day.contains("-")){
                tian.setText(day);
            }else{
                tian.setText("距离计划完成还有"+day+"天");
            }
        }
        if(flag == 3){
            isDone.setText("完成任务并发送给好友");
            task_editor_tv.setVisibility(View.INVISIBLE);
            if(!dataBean.isIsComplete()){
               if(dataBean.getBookPlace().length()==0||dataBean.getRemarks().length()==0){
                   isDone.setTextColor(getResources().getColor(R.color.colorRed4));
               }else{
                   isDone.setTextColor(getResources().getColor(R.color.colorWhite));
                   //可以发送给好友已经完成了;
                   isDone.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View v) {
                           //发送给好友完成任务;
                           update_iscomplete();
                       }
                   });
               }
                if(dataBean.getBookPlace().length()!=0){
                    bookPlace.setText(dataBean.getBookPlace());
                }
                //
                job_detail_nextto.setVisibility(View.VISIBLE);

                findViewById(R.id.job_detail_location_group).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        Intent intent = new Intent(JobDetail.this,LocalActivity.class);
//                        intent.putExtra(JobDetail.TASK_FLAG,dataBean.getBookTitle());
//                        intent.putExtra("JobDetail",dataBean);
//                        startActivity(intent);
                        GoToLocal();
                    }
                });
                findViewById(R.id.job_detail_missionPersonnel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent remarkIntent = new Intent(JobDetail.this,NoticeActivity.class);
                        Bundle bundle1 = new Bundle();
                        bundle1.putSerializable(JobDetail.TASK_FLAG,dataBean);
                        remarkIntent.putExtras(bundle1);
                        startActivityForResult(remarkIntent,6);
                    }
                });

            }

        }else if (flag == 7) {// 从计划书中过来的
            if ("".equals(dataBean.getNvc_Name())){
                task_editor_tv.setVisibility(View.VISIBLE);
                isDone.setText("未完成任务");
            }else{
                if (dataBean.isIsComplete()){
                    task_editor_tv.setVisibility(View.VISIBLE);
                    isDone.setText("已完成任务");
                }else {
                    task_editor_tv.setVisibility(View.INVISIBLE);
                    isDone.setText("未完成任务");
                }
            }

        }else {
            if (dataBean.getNvc_Name() == null&&"".equals(dataBean.getNvc_Name())) {
                //编辑按钮消失;
                task_editor_tv.setVisibility(View.INVISIBLE);
                //判断是否完成;
                if (dataBean.isIsComplete()) {
                    task_editor_tv.setVisibility(View.INVISIBLE);
                    isDone.setText("已经完成任务");
                } else {
                    task_editor_tv.setVisibility(View.VISIBLE);
                    isDone.setText("未完成任务");
                }
            }
        }
    }

    public  void  onBack(View view){
        if (flag==3){
            Intent intent = new Intent();
            setResult(1,intent);
            this.finish();
            return;
        }
        MessageEvent event = new MessageEvent();
        event.setFlash("开始刷新");
        EventBus.getDefault().post(event);
        Intent intent = new Intent();
        setResult(10,intent);
        this.finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if (data!=null) {
                    dataBean = (MarriageProposalBean.DataBean) data.getSerializableExtra(TASK_FLAG);
                    changContent(dataBean);
                    HEADName = dataBean.getWeddingBookID() + ".jpg";
                    if (headBitmap != null) {
                        headBitmap = null;
                        headBitmap = FileUtil.getBitmapFromDisk(HEADName, this);
                        circularImage.setImageBitmap(headBitmap);
                    } else {
                        headBitmap = FileUtil.getBitmapFromDisk(HEADName, this);
                        circularImage.setImageBitmap(headBitmap);
                    }
                }
                break;
            case 6:
                if (data!=null) {
                    dataBean = (MarriageProposalBean.DataBean) data.getSerializableExtra(JobDetail.TASK_FLAG);
                    notice.setText(dataBean.getRemarks());
                    changContent(dataBean);
                }
                break;
            case 8:
                if (data!=null){
                    dataBean = (MarriageProposalBean.DataBean) data.getSerializableExtra(JobDetail.TASK_FLAG);
                    bookPlace.setText(dataBean.getBookPlace());
                }
                break;
            }
    }
    private String place;
    @Subscribe
    public void onEvent(MessageEvent event){
        place = event.getPlace();
        bookPlace.setText(place);
        dataBean.setBookPlace(place);
        changContent(dataBean);
    }

    private CommonResultBean bean;
    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if(data instanceof  CommonResultBean){
            bean = (CommonResultBean) data;
            switch (bean.getResCode()){
                case "0000":
                    CUtils.showToast("发送成功了");
                    finish();
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

    /**
     * POST Wedding/Update_IsComplete?Token={Token}&i_Id={i_Id}
     修改流程是否完成
     */
    public void update_iscomplete(){
       String useUrl =Url.BASE+"Update_IsComplete?Token="+"00&i_Id="+dataBean.getMemberID();
        HashMap<String,String> paramsMap=new HashMap<>();
        paramsMap.put("BookTitle",dataBean.getBookTitle());
        paramsMap.put("IsComplete","1");
        paramsMap.put("AssistantID",""+dataBean.getMemberID());
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST,useUrl, this, CommonResultBean.class, RequestCode.UPDATE_ISCOMPLETE, paramsMap);
        HttpHandleUtil.executeCommonPost(this,httpRequestBean);
    }

    private void GoToLocal(){
        Intent intent = new Intent(this,LocalActivity1.class);
        intent.putExtra(JobDetail.TASK_FLAG,dataBean);
        startActivityForResult(intent,8);
    }

}


